layui.use(['form','jquery','table','layer'],function(){
    var form = layui.form,
        $ = layui.jquery,
        table = layui.table,
        layer = layui.layer;

    var tabInstance;

    //渲染表格，请求所有区域数据
    tabInstance = table.render({
        elem: '#buildingList',//id选择器
        url: '/broker-building/getBuilding', //数据接口
        page: true, //开启分页
        limit: 5, //每页显示的条数，默认5
        limits: [5, 10, 15,], //每页显示条数选择框数据
        request: {
            pageName: 'current',
            limitName: 'size'
        },
        cols: [[ //表头
            {field: 'brokerBuildingId', title: '楼盘ID', width: 160, sort: true, fixed: 'left', align: 'center'},
            {field: 'brokerBuildingName', title: '楼盘名字',width:120, align: 'center'},
            {field: 'traffic', title: '交通指标',width: 120, align: 'center'},
            {field: 'property', title: '物业管理',width: 120, align: 'center'},
            {field: 'qualityIndex', title: '质量指标',width: 120, align: 'center'},
            {field: 'tenancyAreaId', title: '租赁区域ID',width: 120, align: 'center',hide:true},
            {field: 'tenancyAreaName', title: '租赁区域名称',width: 120, align: 'center'},
            {field: 'saleAreaId', title: '销售区域ID',width: 120, align: 'center',hide:true},
            {field: 'saleAreaName', title: '销售区域名称',width: 120, align: 'center'},
            {field: 'handlerId', title: '添加者ID',width: 120, align: 'center',hide:true},
            {field: 'userName', title: '添加者',width: 120, align: 'center'},
            {field: 'operation',title: '操作', width: 180,fixed:'right',toolbar:"#buildingTool", align: 'center'}
        ]],
        done: function (res) {

        }
    });

    //区域搜索
    form.on('submit(search)',function(data){
        console.log(data);
        tabInstance.reload({
            where :data.field
        });
        return false;
    });

    //改删操作
    table.on('tool(buildingList)',function(obj){
        switch (obj.event){
            case 'edit_btn':
                layer.open({
                    type: 2,
                    content: 'edit.jsp?id=' + obj.data.brokerBuildingId,
                    skin: 'layui-layer-molv',
                    title: "修改区域",
                    area: ["450px", "500px"],
                    btn: ['修改', '关闭'],
                    //弹出层的初始化操作
                    success: function (layero, index) {
                        //获取编辑表单
                        var edit = layer.getChildFrame('body', index).find("#edit");
                        console.log(obj);

                        //获取元素值
                        edit.find("#brokerBuildingName").val(obj.data.brokerBuildingName);
                        edit.find("#traffic").val(obj.data.traffic);
                        edit.find("#property").val(obj.data.property);
                        edit.find("#qualityIndex").val(obj.data.qualityIndex);
                        edit.find("#handlerId").val(obj.data.handlerId);
                    },
                    yes:function (index, layero) {
                        //获取编辑表单
                        var edit = layer.getChildFrame('body', index).find("#edit");

                        //获取元素值
                        var brokerBuildingName = edit.find("#brokerBuildingName").val();
                        var traffic = edit.find("#traffic").val();
                        var property = edit.find("#property").val();
                        var qualityIndex = edit.find("#qualityIndex").val();
                        var tenancyAreaName =edit.find("#tenancyAreaName").val();
                        var saleAreaName =edit.find("#saleAreaName").val();
                        var handlerId = edit.find("#handlerId").val();

                        //把数组转为JSON,请求后台执行添加操作，后台返回数据后显示
                        var data = {"brokerBuildingId":obj.data.brokerBuildingId};
                        edit.serializeArray().forEach(function(val){
                            data[val.name] = val.value;
                            console.log(val.name);
                            console.log(val.value);
                        });
                        $.post('/broker-building/update',data,function(info){
                            layer.close(index);//关闭窗口
                            layer.msg(info.msg);//弹出增加失败或者成功的信息
                            tabInstance.reload();//重新加载表
                        });

                    }
                });
                break;
            case 'del_btn':
                layer.confirm('你确定要删除楼盘【'+obj.data.brokerBuildingName+'】吗？',{icon:3,anim: 4,skin:'layui-layer-molv'},function(){
                    var url = '/broker-building/deleteById/'+obj.data.brokerBuildingId;
                    $.post(url,function(data){
                        console.log(data);
                        tabInstance.reload();//重新加载数据表格
                        layer.msg(data.msg);
                    });
                });
        }
    });

    //添加
    $(".add_btn").click(function(){
        layer.open({
            type: 2,
            content: 'add.jsp',
            skin:'layui-layer-molv',
            title:"添加楼盘",
            area:["450px","600px"],
            btn:['确定','关闭'],
            yes: function(index, layero){
                //获取编辑表单
                var add = layer.getChildFrame('body', index).find("#add");

                //获取元素值
                var brokerBuildingName = add.find("#brokerBuildingName").val();
                var traffic = add.find("#traffic").val();
                var property = add.find("#property").val();
                var qualityIndex = add.find("#qualityIndex").val();
                var tenancyAreaName = add.find("#tenancyAreaName").val();
                var saleAreaName = add.find("#saleAreaName").val();
                var handlerId = add.find("#handlerId").val();

                //把数组转为JSON,请求后台执行添加操作，后台返回数据后显示
                var data = {};
                add.serializeArray().forEach(function(val){
                    data[val.name] = val.value;
                    console.log(val.name);
                    console.log(val.value);
                });

                $.post('/broker-building/add',data,function(info){
                    layer.close(index);//关闭窗口
                    layer.msg(info.msg);//弹出增加失败或者成功的信息
                    tabInstance.reload();//重新加载表
                });
            }
        });
    });
});